﻿using AutoMapper;
using Entity.System.DTO.CodeRule;
using Entity.System.Entity;
using Entity.System.View.CodeRule;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace Service.System
{
    public class CodeRuleService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public CodeRuleService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取编码规则列表
         */
        public async Task<RequestResponseModel<PageResult<CodeRuleView>>> GetCodeRuleList(CodeRuleSearchDTO codeRuleSearchDTO)
        {
            //筛选
            var query = _context.CodeRuleView
                .WhereIF(a => a.Code.Contains(codeRuleSearchDTO.Code), !string.IsNullOrEmpty(codeRuleSearchDTO.Code))
                .WhereIF(a => a.Rule.Contains(codeRuleSearchDTO.Rule), !string.IsNullOrEmpty(codeRuleSearchDTO.Rule))
                ;

            //返回列表
            var codeRuleList = await query
                .OrderBy(a => a.CreateTime)
                .Page(codeRuleSearchDTO.CurrentPage, codeRuleSearchDTO.PageSize)
                .ToListAsync();

            //计算总数
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<CodeRuleView>(codeRuleList, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增编码规则
         */
        public async Task<RequestResponseModel<string>> AddCodeRule(CodeRuleAddDTO codeRuleAddDTO)
        {
            //校验数据
            var query = await Valid.ValidId<CodeRuleModel>(_context.CodeRule, "132139f6-1a00-4094-8188-e7047fe5cb3f");
            if (query == null)
            {
                return RequestResponse.CreateResponse(500, "源数据已被清除，请联系管理员", "");
            };

            //新增数据
            var codeRule = _mapper.Map<CodeRuleModel>(codeRuleAddDTO);
            codeRule.Code = query.Rule + query.Number.ToString();
            codeRule.CreateTime = DateTime.Now;
            codeRule.Id = Guid.NewGuid().ToString();

            //更新表
            _context.CodeRule.Update(codeRule);
            _context.CodeRule.Add(codeRule);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "新增成功", codeRule.Id);
        }

        /**
         * 更新编码规则
         */
        public async Task<RequestResponseModel<string>> PutCodeRule(CodeRuleUpdateDTO codeRuleUpdateDTO)
        {
            //校验数据
            var query = await Valid.ValidId<CodeRuleModel>(_context.CodeRule, codeRuleUpdateDTO.Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", codeRuleUpdateDTO.Id);
            };

            //编辑数据
            var menu = _mapper.Map(codeRuleUpdateDTO, query);

            //更新表
            _context.CodeRule.Update(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "更新成功", codeRuleUpdateDTO.Id);
        }

        /**
         * 删除编码规则
         */
        public async Task<RequestResponseModel<string>> DeleteCodeRule(string Id)
        {
            //校验数据
            var query = await Valid.ValidId<CodeRuleModel>(_context.CodeRule, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            };

            //更新表
            _context.CodeRule.Remove(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }

        /**
         * 批量删除
         */
        public async Task<RequestResponseModel<List<string>>> DeleteCodeRuleMulti(List<string> Ids)
        {
            //是否有匹配资源
            var query = await Valid.ValidIdList<CodeRuleModel>(_context.CodeRule, Ids);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Ids无效", Ids);
            }

            //更新表
            _context.CodeRule.RemoveRange(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "删除成功", Ids);
        }

        /**
         * 获取编码
         */
        public async Task<RequestResponseModel<string>> GetNewCode(string Id)
        {
            //校验数据
            var query = await Valid.ValidId<CodeRuleModel>(_context.CodeRule, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            };

            //生成编码
            query.Number++;

            //更新表
            _context.CodeRule.Update(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "获取成功", query.Rule + query.Number);
        }
    }
}
